<template>
  <Dialog :title="title" v-model="visible" width="90%">
    <el-form ref="formRef" :model="form" :rules="formRules" v-loading="loading" label-width="120px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item align="left" label="入库数量" prop="instockQuantity">
            <el-input-number v-model="form.instockQuantity" :min="1" label="请输入正整数" />
          </el-form-item>
        </el-col>
        
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts" name="GoodsSpuForm"> 
const message = useMessage() // 消息弹窗

const visible = ref(false) // 弹窗的是否展示
const title = ref(`编辑归还数量`) // 弹窗的标题
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用

const form = ref({
  type: 'radio',
  id: '',
  name: '',
  quantity: 1,
}) 

const formRules = reactive({
  quantity: [{ required: true, message: '数量不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

let quantity = 0

/** 打开弹窗 */
const open = async (item) => {
  visible.value = true

  form.value = item

  quantity = item.quantity
  
}

defineExpose({ open }) // 提供 open 方法,用于打开弹窗


/** 提交表单 */
const emit = defineEmits(['updateInstockQuantitySuccess']) // 定义 updateItemSuccess 事件,用于操作成功后的回调
const submitForm = async () => {

  if (!(form.value.instockQuantity) || form.value.instockQuantity < 1 || form.value.instockQuantity > quantity) {

    return message.error(`操作失败, 归还数量不正确，请确认信息`)
    
  }

  // 提交请求
  loading.value = true 
  message.success(`操作成功`)
  visible.value = false
  loading.value = false
  // 发送操作成功的事件
  emit('updateInstockQuantitySuccess', form.value) 

}

/** 重 置表单 */
const resetForm = () => {
  form.value = {
    type: 'radio',
    id: '',
    
    quantity: 1,
    
  }
  formRef.value?.resetFields()
}
</script>
